package com.kiell.gulimall.search.controller;

import com.kiell.common.es.SkuEsModel;
import com.kiell.common.exception.BizCodeEnum;
import com.kiell.common.utils.R;
import com.kiell.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: kiell
 * @createTime: 2024年2月6日11:45:57
 **/


@Slf4j
@RequestMapping(value = "/search/save")
@RestController
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;
//
//
    /**
     * 上架商品
     * @param skuEsModels
     * @return
     */
    @PostMapping(value = "/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {

        boolean status=false;
        try {
            status = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("商品上架错误{}",e);

            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if(status){
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }else {
            return R.ok();
        }

    }

    /**
     * 下架商品
     * @param spuId
     * @return
     */
    @PostMapping(value = "/productDown/{spuId}")
    public R productStatusDown(@PathVariable("spuId") Long spuId) {

        boolean status=false;
        try {
            status = productSaveService.productStatusDown(spuId);
        } catch (IOException e) {
            log.error("商品下架错误{}",e);

            return R.error(BizCodeEnum.PRODUCT_DOWN_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_DOWN_EXCEPTION.getMsg());
        }

        if(status){
            return R.ok();
        }else {
           return R.error(BizCodeEnum.PRODUCT_DOWN_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_DOWN_EXCEPTION.getMsg());
        }

    }


}
